package cn.s1995.sloth.app.controller;

import java.util.HashMap;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import cn.s1995.sloth.bizs.constant.BizCst;
import cn.s1995.sloth.bizs.entity.Message;
import cn.s1995.sloth.bizs.oo.req.MessageREQ;
import cn.s1995.sloth.bizs.oo.vo.MessageVO;
import cn.s1995.sloth.bizs.service.MessageService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * 消息
 *
 * @author sloth
 * @date 2024-01-06 10:01:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/m/message")
@Tag(description = "Message", name = "APP-消息")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ApiMessageController {

    private final MessageService messageService;

    /**
     * 获取收到的信息分页
     * @param page 分页对象
     * @param req 消息
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "分页获取接收的信息", description = "分页获取接收的信息")
    @GetMapping("/page")
    public R<Page<MessageVO>> getReceivePage(@ParameterObject Page page, @ParameterObject MessageREQ req) {
        return R.ok(messageService.getReceivePage(page, req));
    }


	@ApiOperationSupport(order = 10)
    @Operation(summary = "批量已读", description = "批量已读")
    @PutMapping("/readBatch")
    public R readBatch(@RequestBody MessageREQ req) {
		messageService.update(
				Wrappers.lambdaUpdate(Message.class)
						.set(Message::getHasRead, true)
						.eq(Message::getToId, SecurityUtils.getUser().getId())
						.eq(Message::getType, req.getType())
		);
        return R.ok();
    }

	@ApiOperationSupport(order = 20)
    @Operation(summary = "查询未读数量", description = "查询未读数量")
    @GetMapping("/unreadCount")
    public R unreadCount() {
		HashMap<String, Long> res = new HashMap<String, Long>();
		long orderUnreadCount = messageService.count(
				Wrappers.lambdaQuery(Message.class)
						.eq(Message::getToId, SecurityUtils.getUser().getId())
						.eq(Message::getType, BizCst.Message.Type.order)
						.eq(Message::getHasRead, false)
		);
		long systemUnreadCount = messageService.count(
				Wrappers.lambdaQuery(Message.class)
						.eq(Message::getToId, SecurityUtils.getUser().getId())
						.eq(Message::getType, BizCst.Message.Type.system)
						.eq(Message::getHasRead, false)
		);
		res.put("order", orderUnreadCount);
		res.put("system", systemUnreadCount);
        return R.ok(res);
    }

//	/**
//	 * 批量已读
//	 * @param ids
//	 * @return
//	 */
//	@ApiOperationSupport(order = 10)
//    @Operation(summary = "批量已读", description = "批量已读")
//    @PutMapping("/readBatch")
//    public R readBatch(@RequestBody List<Long> ids) {
//		messageService.readBatch(ids);
//        return R.ok();
//    }
}
